package com.hunk.word;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

/**
 * POI Word Service
 * 
 * @author Dengyx
 * 
 */
public class PoiService {
	@SuppressWarnings("unused")
	private static class Test {
		public static void main(String[] args) {
			writeWordFile();
		}

		public static void readWordFile() {
			FileInputStream in = null;
			try {
				in = new FileInputStream("D:/2003.doc");
				try {
					POIFSFileSystem pfs = new POIFSFileSystem(in);
					HWPFDocument hwpf = new HWPFDocument(pfs);
					Range range = hwpf.getRange();// 得到文档的读取范围
					short column = 4;
					int row = 5;
					Table table = range.insertTableBefore(column, row);
					TableIterator it = new TableIterator(range);
					while (it.hasNext()) {
						Table tb = (Table) it.next();
						// 迭代行，默认从0开始
						for (int i = 0; i < tb.numRows(); i++) {
							TableRow tr = tb.getRow(i);
							// 迭代列，默认从0开始
							for (int j = 0; j < tr.numCells(); j++) {
								TableCell td = tr.getCell(j);// 取得单元格
								// 取得单元格的内容
								for (int k = 0; k < td.numParagraphs(); k++) {
									Paragraph para = td.getParagraph(k);
									// para.insertAfter("5555");
									String s = para.text().trim();
									System.out.println(s);
								} // end for
							} // end for
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} finally {
				try {
					if (in != null) {
						in.close();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}

		public static void writeWordFile() {
			try {
				//创建配置实例
				Configuration cfg = new Configuration();
				cfg.setDefaultEncoding("utf-8");
				cfg.setDirectoryForTemplateLoading(new File("C://wtt"));
				cfg.setObjectWrapper(new DefaultObjectWrapper());

				//获取模板
				Template temp = cfg.getTemplate("demo.ftl");
				temp.setEncoding("utf-8");
				
				//创建数据模型
				Map<String,Object> root = new HashMap<String,Object>();
				root.put("stringdemo", "字符串测试");
				List<List<String>> rows = new ArrayList<List<String>>();
				for (int j=1; j<5; j++) {
					List<String> row = new ArrayList<String>();
					for (int i=1; i<16; i++) {
						row.add(j+""+i);
					}
					rows.add(row);
				}
				root.put("rows", rows);
				
				//将模板和数据模型合并生成文件
				File docFile = new File("C://wtt/demo.doc");
				Writer docout = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
				temp.process(root, docout);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
